<?php 
defined('RUN_KATANA') or die('Restricted access');
class Ship extends Object
{
	public $Id;
	public $SiteId;
	public $ParentId;
	public $IdentityNumber;
	public $Name;
	public $ShipOrder;
	public $Active;
	public $DateCreated;
	public $DateModified;
	public $DataChanged;
	
	public $ScheduleItems;
	public $ScheduleItemsLoaded;
	
	public function getId() { return $this->Id; }
	public function getSiteId() { return $this->SiteId; }
	public function getParentId() { return $this->ParentId; }
	public function getIdentityNumber() { return $this->IdentityNumber; }
	public function getName() { return $this->Name; }
	public function getShipOrder() { return $this->ShipOrder; }
	public function getActive() { return $this->Active; }
	public function getDateCreated() { return $this->DateCreated; }
	public function getDateModified() { return $this->DateModified; }
	public function getDataChanged() { return $this->DataChanged; }
	public function getScheduleItems() { return $this->ScheduleItems; }
	public function getScheduleItemsLoaded() { return $this->ScheduleItemsLoaded; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setParentId($val) { $this->ParentId=$val; }
	public function setIdentityNumber($val) { $this->IdentityNumber=$val; }
	public function setName($val) { $this->Name=$val; }
	public function setShipOrder($val) { $this->ShipOrder=$val; }
	public function setActive($val) { $this->Active=$val; }
	public function setDateCreated($val) { $this->DateCreated=$val; }
	public function setDateModified($val) { $this->DateModified=$val; }
	public function setDataChanged($val) { $this->DataChanged=$val; }
	public function setScheduleItems($val) { $this->ScheduleItems=$val; }
	public function setScheduleItemsLoaded($val) { $this->ScheduleItemsLoaded=$val; }	
	
	public function update()
	{
		if(($this->Id > 0) && ($this->DataChanged == true))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'fleet_ship SET ';
			$sql .= '`identity_number`='.$this->IdentityNumber.', ';
			$sql .= '`name`="'.$this->Name.'", '; 
			if(is_numeric($this->ShipOrder))
			{ $sql .= '`ship_order`='.intval($this->ShipOrder).', '; }
			$sql .= '`date_modified`=NOW(), ';
			$sql .= '`active`='.$this->Active.' ';
			$sql .= 'WHERE `id`='.$this->Id.';';
			$result = mysql_query($sql);
		}
	}
	
	public function loadScheduleItems() 
	{
		if(($this->Id > 0) && (self::getScheduleItemsLoaded() == false))
		{
			$sql  = ' SELECT s.*, c.printable_name FROM '.DB_PERFIX.'fleet_ship_schedule AS s ';
			$sql .= ' LEFT JOIN '.DB_PERFIX.'country AS c ON s.country=c.iso ';
			$sql .= ' WHERE s.ship_id='.$this->Id;
			if($this->View == 'frontend')
			{
				$sql .= ' AND s.archived=0';
				$sql .= ' ORDER BY s.eta ASC;';
			}
			else
			{ $sql .= ' ORDER BY s.eta DESC;'; }
			
			$result = mysql_query($sql); 
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->ScheduleItems[$row['id']] = new ScheduleItem;
					$this->ScheduleItems[$row['id']]->setId($row['id']);
					$this->ScheduleItems[$row['id']]->setShipId($row['ship_id']);
					$this->ScheduleItems[$row['id']]->setCountry($row['country']);
					$this->ScheduleItems[$row['id']]->setCountryName($row['printable_name']);
					$this->ScheduleItems[$row['id']]->setPort($row['port']);
					$this->ScheduleItems[$row['id']]->setEta($row['eta']);
					$this->ScheduleItems[$row['id']]->setEtd($row['etd']);
					$this->ScheduleItems[$row['id']]->setArchived($row['archived']);
				}
			}
			self::setScheduleItemsLoaded(true);
		}
	}
}
?>